﻿from django.shortcuts import render, HttpResponse


# Create your views here.
def index(request):
    '''首页

    准备工作
    '''
    markdown = """# Chapter 1

## 主从路由配置

* APP内 `/chapter1/views.py` 添加视图函数 `index_handler`

    ```python
    from django.shortcuts import HttpResponse
    def index_handler(request):
        return HttpResponse('Hello Chapter 1')
    ```

* 主路由 `/work03/urls.py` 添加映射配置到 `chapter1`

    ```python
    from django.urls import include
    urlpatterns = [
        ...
        path('', include('chapter1.urls')),
    ]
    ```

* 从路由创建 `/chapter1/urls.py`

    ```python
    from django.urls import path
    from . import views

    urlpatterns = [
        path('', views.index_handler),
    ]
    ```

## 命名空间


```python
# /chapter1/views.py 视图函数
from django.shortcuts import HttpResponse
def index(request):
    return HttpResponse('Hello chapter1')
```

```python
# /chapter1/urls.py 从路由
from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
]
```

* 主路由

    ```python
    # path('【app名字】/', include(('【app.urls.py地址】', '【app名字】'), namespace='【命名空间】')),
    urlpatterns = [
        ...
        path('1/', include(('chapter1.urls', '1'), namespace='1')),
    ]
    ```
"""
    context = {
        'title': 'Home',
        'markdown': markdown,
    }
    return render(request, 'marked.html', context)
